/**
 * 
 */
package br.com.ema.maze.factory;

import java.awt.Color;
import java.util.List;

import br.com.ema.maze.agents.MazeCharacter;
import br.com.ema.maze.character.strategy.AStarStrategy;
import br.com.ema.maze.character.strategy.GreedyStrategy;
import br.com.ema.maze.components.Coordinates;
import br.com.ema.maze.components.Maze;
import br.com.ema.maze.components.MazeSpace;
import br.com.ema.maze.components.MazeWall;

/**
 * @author Emanuel Cruz Rodrigues -> emanuelcruzrodrigues@gmail.com
 *
 */
public class MazeFileFactory extends MazeAbstractFactory<MazeFile>{
	
	private MazeNearbyUpdater updater = new MazeNearbyUpdater();
	private MazeConfiguration mazeConfiguration;

	/* (non-Javadoc)
	 * @see br.com.ema.maze.factory.MazeAbstractFactory#buildMaze(java.lang.Object)
	 */
	@Override
	public Maze buildMaze(MazeFile mazeFile) {
		Maze maze = new Maze();
		mazeConfiguration = new MazeConfiguration();
		
		MazeCharacter character1 = new MazeCharacter(250);
		character1.setStrategy(new AStarStrategy(10000));
		character1.setColor(Color.CYAN);
		mazeConfiguration.getCharacters().put("ASTAR",character1);

		MazeCharacter character2 = new MazeCharacter(250);
		character2.setStrategy(new GreedyStrategy());
		character2.setColor(Color.ORANGE);
		mazeConfiguration.getCharacters().put("GREEDY", character2);

		
		mazeConfiguration.setPassageAllowersInterval(1000);
		
		putMazeSpacesSettingExitAndStart(mazeFile, maze, mazeConfiguration);
		
		setNearbyInToSpaces(maze);
		
		return maze;
	}

	private void putMazeSpacesSettingExitAndStart(MazeFile mazeFile, Maze maze, MazeConfiguration mazeConfiguration) {
		List<String> fileLines = mazeFile.getFileLines();
		
		for (int y = 0; y < fileLines.size(); y++) {
			
			String line = fileLines.get(y);
			
			for (int x = 0; x < line.length(); x++) {
				char symbol = line.charAt(x);
				MazeSpace space = createSpace(symbol, x, y);
				maze.addSpace( space );
				if (symbol == 'E'){
					mazeConfiguration.setExitCoordinates(new Coordinates(x, y));
				}else if (symbol == 'S'){
					mazeConfiguration.setStartCoordinates(new Coordinates(x, y));
				}
			}
			
		}
	}
	
	private void setNearbyInToSpaces(Maze maze) {
		updater.updateNearby(maze);	
	}


	private MazeSpace createSpace(char symbol, int x, int y){
		MazeSpace result = new MazeSpace(x, y);

		switch (symbol) {
		case 'W':
			result.setDecoration(new MazeWall());
			break;
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see br.com.ema.maze.factory.MazeAbstractFactory#getMazeConfiguration(java.lang.Object)
	 */
	@Override
	public MazeConfiguration getMazeConfiguration(MazeFile mazeFile) {
		return mazeConfiguration;
	}

}
